package com.auth.service.impl;

import com.auth.bean.MenusBean;
import com.auth.mapper.BsSystemMenuItemsMapper;
import com.auth.mapper.BsSystemMenusMapper;
import com.auth.model.BsSystemMenuItems;
import com.auth.model.BsSystemMenus;
import com.auth.service.MenuServiceI;
import com.exception.bean.Result;
import com.exception.enu.ExceptionEnum;
import com.exception.util.ResultUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

/**
 * <dl>
 * <dt>MenuServiceImpl</dt>
 * <dd>Description: 菜单接口实现类</dd>
 * <dd>CreateDate: 2017/8/21</dd>
 * </dl>
 *
 * @author luo
 */
@Service
public class MenuServiceImpl implements MenuServiceI {

    @Autowired
    private BsSystemMenuItemsMapper bsSystemMenuItemsMapper;

    @Autowired
    private BsSystemMenusMapper bsSystemMenusMapper;

    /**
     * 删除菜单
     *
     * @param id     标示ID
     * @param unitid 单位ID
     * @param userId 用户ID (只有超级管理员有权限删除)
     * @return
     */
    public Result deleteMenusItemById(String id,String unitid,String userId) {
        BsSystemMenuItems items = bsSystemMenuItemsMapper.selectByPrimaryKey(id);
        if(items != null && items.getUnitid().equals(unitid)){
            int i = bsSystemMenuItemsMapper.deleteByPrimaryKey(id);
            if (i == 1) return ResultUtil.success(i);
        }
        return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
    }

    /**
     * 添加菜单功能项
     *
     * @param mid    所属的系统功能菜单项ID
     * @param name   功能项名称
     * @param unitid 单位ID
     * @return
     */
    public Result addMenusItem(String mid, String name,String unitid) {
        try {
            BsSystemMenus menus = bsSystemMenusMapper.selectByPrimaryKey(mid);
            if(menus == null || !menus.getUnitid().equals(unitid.trim())){
                return ResultUtil.error(ExceptionEnum.PARA_ERROR);
            }
            BsSystemMenuItems menu = new BsSystemMenuItems(UUID.randomUUID().toString(), mid, name,unitid);
            int i = bsSystemMenuItemsMapper.insert(menu);
            if (i == 1) return ResultUtil.success(i);
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        } catch (Exception e) {
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        }
    }

    /**
     * 修改系统功能项信息表
     *
     * @param id     标示ID
     * @param menuId 功能项ID
     * @param name   功能项名称
     * @param unitid 单位ID
     * @return
     */
    public Result updateMenusItemMenuIdOrNameById(String id, String menuId, String name,String unitid) {
        try {
            if ((StringUtils.isEmpty(menuId) || StringUtils.isEmpty(menuId.trim()))
                    && (StringUtils.isEmpty(name) || StringUtils.isEmpty(name.trim()))) {
                return ResultUtil.error(ExceptionEnum.PARA_ERROR);
            }
            BsSystemMenuItems items = new BsSystemMenuItems(id, menuId, name,unitid);
            int i = bsSystemMenuItemsMapper.updateItemsByIdAndUnitid(items);
            if (i == 1) return ResultUtil.success(i);
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        } catch (Exception e) {
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        }
    }

    /**
     * 菜单功能项分页查询
     *
     * @param pageNo   页数
     * @param pageSize 行数
     * @param unitid   单位ID
     * @return
     */
    public Result findMenusItemsPageByCondition(int pageNo, int pageSize,String unitid) {
        try {
            BsSystemMenuItems items = new BsSystemMenuItems();
            items.setUnitid(unitid);
            PageHelper.offsetPage(pageNo,pageSize);
            List<BsSystemMenuItems> itemsList = bsSystemMenuItemsMapper.findMenusItemsByCondition(items);
            return ResultUtil.success(itemsList);
        } catch (Exception e) {
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        }
    }

    /**
     * 菜单功能项查询
     *
     * @param unitid 单位ID
     * @param mid    菜单ID
     * @return
     */
    public Result findMenusItemsByCondition(String unitid, String mid) {
        try {
            BsSystemMenuItems items = new BsSystemMenuItems();
            items.setUnitid(unitid);
            items.setMid(mid);
            List<BsSystemMenuItems> itemsList = bsSystemMenuItemsMapper.findMenusItemsByCondition(items);
            return ResultUtil.success(itemsList);
        }catch (Exception e){
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        }
    }

    /**
     * 添加菜单
     *
     * @param unitid 单位ID
     * @param name   功能菜单名称
     * @param pid    上级功能菜单ID
     * @param type   应用类型（APP、PC）
     * @return
     */
    public Result addMenus(String unitid, String name, String pid, Integer type) {
        try {
            String id = UUID.randomUUID().toString();
            BsSystemMenus menus = new BsSystemMenus(id, unitid, name, pid, type);
            int i = bsSystemMenusMapper.insert(menus);
            if (i == 1) return ResultUtil.success(i);
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        } catch (Exception e) {
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        }
    }

    /**
     * 删除菜单
     *
     * @param id     菜单ID
     * @param userId 管理员ID
     * @param unitid 单位ID
     * @return
     */
    public Result deleteMenusById(String id,String unitid,String userId) {
        try {
            //没有下级菜单的菜单才能删除
            BsSystemMenus menus = bsSystemMenusMapper.selectByPrimaryKey(id);
            if (menus != null && menus.getUnitid().equals(unitid)) {
                BsSystemMenus params = new BsSystemMenus(null, unitid, null, menus.getId(), null);
                List<BsSystemMenus> secondaryMenu = bsSystemMenusMapper.selectMenusByCondition(params);
                if (secondaryMenu != null && secondaryMenu.size() > 0) {
                    return ResultUtil.error(-3, "请先删除下级菜单");
                }
                int i = bsSystemMenusMapper.deleteByPrimaryKey(id);
                if (i == 1) {
                    return ResultUtil.success(i);
                }
            }
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        } catch (Exception e) {
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        }
    }

    /**
     * 修改菜单名称
     *
     * @param id     菜单ID
     * @param unitid 单位ID
     * @param name   菜单名称
     * @return
     */
    public Result updateMenusNameById(String id, String unitid, String name) {
        try {
            BsSystemMenus params = new BsSystemMenus(id, unitid, name, null, null);
            int i = bsSystemMenusMapper.updateByPrimaryKeySelective(params);
            if (i == 1) {
                return ResultUtil.success(i);
            }
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        } catch (Exception e) {
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        }
    }

    /**
     * 根据条件查菜单
     *
     * @param id     菜单ID
     * @param unitid 单位ID
     * @param pid    上级功能菜单ID
     * @param type   应用类型（APP、PC）
     * @return
     */
    public Result findMenusByCondition(String id, String unitid, String pid, Integer type) {
        try {
            BsSystemMenus params = new BsSystemMenus(id, unitid, null, pid, type);
            List<BsSystemMenus> menusList = bsSystemMenusMapper.selectMenusByCondition(params);
            return ResultUtil.success(menusList);
        } catch (Exception e) {
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        }
    }

    /**
     * 分页查询菜单
     *
     * @param pageNo   页数
     * @param pageSize 行数
     * @param menus    参数
     * @return
     */
    public Result findMenusPageByCondition(int pageNo, int pageSize, BsSystemMenus menus) {
        try {
            PageHelper.offsetPage(pageNo,pageSize);
            List<BsSystemMenus> menusList = bsSystemMenusMapper.selectMenusByCondition(menus);
            return ResultUtil.success(menusList);
        } catch (Exception e) {
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        }
    }

    /**
     * 菜单功能项关联详细信息查询(对外)
     *
     * @param id     菜单ID
     * @param unitid 单位ID
     * @return
     */
    public Result findItemsInfoByCondition(String id, String unitid) {
        try {
            BsSystemMenus params = new BsSystemMenus(id, unitid, null, null, null);
            List<MenusBean> items = bsSystemMenusMapper.findMenusItems(params);
            return ResultUtil.success(items);
        }catch (Exception e){
            return ResultUtil.error(ExceptionEnum.UNKNOW_ERROR);
        }
    }
}
